<template>
  <el-card>
    <div slot="header" class="clearfix"></div>
    <el-table
      :data="proTypeList"
      border
      style="width: 100%"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column prop="productTypeId" label="产品类型编号" width="200px">
      </el-table-column>
      <el-table-column
        prop="productTypeName"
        label="产品类型名称"
        width="500px"
      >
      </el-table-column>
      <el-table-column label="操作" width="400px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleEdit(scope.row.productTypeId)"
            >编辑</el-button
          >
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.productTypeId)"
          >
            <el-button
              type="danger"
              slot="reference"
              size="small"
              icon="el-icon-delete"
            >
              删除
            </el-button>
          </el-popconfirm>
          <el-button type="success" size="small" @click="dialog.addShow = true">
            新建
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- --------------------------添加产品类型---------------------- -->
    <el-dialog title="添加产品" :visible.sync="dialog.addShow" width="30%">
      <el-form ref="addForm" :model="addForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="类型名称">
              <el-input
                v-model="addForm.productTypeName"
                placeholder="请输入产品类型名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addShow = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- --------------------------修改产品类型---------------------- -->
    <el-dialog title="编辑工单" :visible.sync="dialog.updateShow" width="30%">
      <el-form ref="updateForm" :model="updateForm" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="类型名称">
              <el-input
                v-model="updateForm.productTypeName"
                placeholder="请输入产品类型名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="编号" hidden>
              <el-input v-model="updateForm.productTypeId"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateShow = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      updateForm: {},
      addForm: {},
      dialog: {
        addShow: false,
        updateShow: false,
      },
      proTypeList: [],
    };
  },
  methods: {
    loadProType() {
      this.$axios.post("protype/queryAll").then((res) => {
        this.proTypeList = res.data.data.list;
      });
    },

    add() {
      this.$axios.post("protype", this.addForm).then((res) => {
        if (res.data.code == 200) {
          this.addForm = {};
          this.$message.success(res.data.message);
          this.dialog.addShow = false;
          this.loadProType();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    handleEdit(productTypeId) {
      this.$axios.get("protype/" + productTypeId).then((res) => {
        this.updateForm = res.data.data.type;
      });
      this.dialog.updateShow = true;
    },
    update() {
      this.$axios.put("protype", this.updateForm).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.dialog.updateShow = false;
          this.loadProType();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    handleDelete(productTypeId) {
      this.$axios.delete("protype/" + productTypeId).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.loadProType();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
  },
  mounted() {
    this.loadProType();
  },
};
</script>